/**
 * Created by yuris on 18.02.15.
 */
package model.objects {
import model.Constants;
import model.prototypes.ShelfProto;
import model.prototypes.ShopObjectProto;

public class ShelfData extends ShopObjectData {
    private var _products:Vector.<ProductData>;
    public function ShelfData(proto:ShopObjectProto) {
        super(proto, null);
        this._products = new Vector.<ProductData>(ShelfProto(proto).partsNum);
    }

    public function setProductOn(shelfPartNum:int,product:ProductData):void {
        this.deleteProductOn(shelfPartNum);
        this._products[shelfPartNum] = product;
        updateSignal.sendSignal(Constants.OBJECT_SHELF_SET_PRODUCT_SIGNAL,{shelf_part:shelfPartNum, product:product})
    }

    private var _bufProduct:ProductData;
    public function getUncompletedSlotNum(productId:String):int {
        for (var i:int = 0; i < _products.length; i++) {
            this._bufProduct = this._products[i];
            if(this._bufProduct && this._bufProduct.proto.productId == productId){
                if(this._bufProduct.count < int((super.proto as ShelfProto).partVolume/this._bufProduct.proto.volume)){
                    return i;
                }
            }
        }
        return -1;
    }
    public function getFreeSlotNum():int {
        for (var i:int = 0; i < _products.length; i++) {
            if(this._products[i] == null || this._products[i].count == 0){
                return i;
            }
        }
        return -1;
    }

    public function updateProductAmountOn(shelfPartNum:int, value:int):void {
        if(this._products[shelfPartNum]){
            this._products[shelfPartNum].count = value
            updateSignal.sendSignal(Constants.OBJECT_SHELF_PRODUCT_UPDATE_SIGNAL,{shelf_part:shelfPartNum, value:value});
        }
    }

    public function deleteProductOn(shelfPartNum:int):Boolean {
        if(shelfPartNum >= 0 && shelfPartNum<this._products.length && this._products[shelfPartNum] != null){
            this._products[shelfPartNum] = null;
            updateSignal.sendSignal(Constants.OBJECT_SHELF_DELETE_PRODUCT_SIGNAL,{shelf_part:shelfPartNum});
            return true;
        }
        return false;
    }

    public function get products():Vector.<ProductData> {
        return _products;
    }
}
}
